<?php

require_once("lib/exception/duplicate_instance_exception.php");
require_once("lib/exception/internal_error_exception.php");
require_once("lib/exception/instance_not_found_exception.php");
require_once("lib/exception/sql_exception.php");

require_once("util/connection.php");

require_once("model/article_image/to/article_image_to.php");
require_once("model/article_image/dao/article_image_dao.php");


class Mysql_article_image_dao extends Article_image_dao{

	public function create($article_image_to) {
		if ($this->exist($article_image_to->get_number())) {
			throw new Duplicate_instance_exception(get_class($this));
		}
		try {
		
			$link = Connection::singleton()->transaction_begin();
			
			$article_link = mysql_real_escape_string($article_image_to->get_link());
			$description = mysql_real_escape_string($article_image_to->get_description());
			$article_number = $article_image_to->get_article_number();
			$autor_login = mysql_real_escape_string($article_image_to->get_autor_login());	
			$date = mysql_real_escape_string($article_image_to->get_date());	
			$state = mysql_real_escape_string($article_image_to->get_state());

			
			
			$result = mysql_query("insert into article_image 
									(link, description, article_number, autor_login, date, state)
									values ('$article_link', '$description', 
									'$article_number', '$autor_login', '$date', '$state')", $link);

			if (!$result || (mysql_affected_rows($link) != 1) 
					|| (($number = mysql_insert_id($link)) == 0)) 
				throw new Sql_exception("Not row inserted in " . get_class($this));
			
			Connection::singleton()->transaction_commit($link);
			
			$article_image_to = new Article_image_to($number, $article_link, $description, $article_number,
															$autor_login, $date, $state);
			return $article_image_to;
		}
		catch (Sql_exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw new Internal_error_exception($e);
		}
		catch (Exception $e) {
			Connection::singleton()->transaction_rollback($link);
			throw new Exception("Internal error exception --> " . $e);
		}
	}
	
	public function exist($number) {

		try {
			$link = Connection::singleton()->transaction_begin();
			
			$result = mysql_query("select * from article_image 
									where number = '$number'", $link);
									
			if (!$result || (mysql_num_rows($result) != 1)) {
				Connection::singleton()->transaction_commit($link);
				return false;
			}
			
			Connection::singleton()->transaction_commit($link);			
			return true;
		}
		catch (Exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw new Internal_error_exception($e);
		}
	}
	
	public function find_by_image_number($number) {
	
		try {
		
			$link = Connection::singleton()->transaction_begin();
			
			$result = mysql_query("select * from article_image 
									where number = '$number'", $link);
									
			if (mysql_num_rows($result) == 0) 
				throw new Instance_not_found_exception(get_class($this));
			
			if (mysql_num_rows($result) != 1) 
				throw new Sql_exception(get_class($this));

			$aux = mysql_fetch_assoc($result);
			Connection::singleton()->transaction_commit($link);
			return new Article_image_to($aux['number'], $aux['link'], $aux['description'],
										$aux['article_number'], $aux['autor_login'], $aux['date'], $aux['state']);
			
		}
		catch (Sql_exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw new Internal_error_exception($e);
		}
		catch (Instance_not_found_exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw $e;
		}
		catch (Exception $e) {
			Connection::singleton()->transaction_rollback($link);
			throw new Exception("Internal error exception --> " . $e);
		}
	}
	
	public function find_by_article_number($article_number) {
		try {
		
			$link = Connection::singleton()->transaction_begin();
			
			$result = mysql_query("select * from article_image 
									where article_number = '$article_number'
									order by date desc", $link);
									
			if (mysql_num_rows($result) == 0) {
				throw new Instance_not_found_exception(get_class($this));
			}
									
			$image_list_to = NULL;
			$i = 0;
			
			while($aux = mysql_fetch_assoc($result))
				$image_list_to[$i++] = new Article_image_to($aux['number'], $aux['link'], $aux['description'],
										$aux['article_number'], $aux['autor_login'], $aux['date'], $aux['state']);
			
			Connection::singleton()->transaction_commit($link);
			return $image_list_to;
			
		}
		catch (Instance_not_found_exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw $e;
		}
		catch (Exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw new Internal_error_exception($e);
		}	
	}

	public function find_last_by_article_number($article_number) {
		try {
			
			$link = Connection::singleton()->transaction_begin();
			
			$result = mysql_query("select * from article_image 
									where article_number = '$article_number'
									order by date desc", $link);
									
			if (mysql_num_rows($result) == 0) {
				throw new Instance_not_found_exception(get_class($this));
			}
			
			$image_to = NULL;
			$i = 0;
						
			if ($aux = mysql_fetch_assoc($result))
				$image_to = new Article_image_to($aux['number'], $aux['link'], $aux['description'],
										$aux['article_number'], $aux['autor_login'], $aux['date'], $aux['state']);
			
			Connection::singleton()->transaction_commit($link);
			return $image_to;
			
		}
		catch (Instance_not_found_exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw $e;
		}
		catch (Exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw new Internal_error_exception($e);
		}	
	}
	
	public function count_images_by_login($login) {
		try {	
			$link = Connection::singleton()->transaction_begin();

			$login = mysql_real_escape_string($login);
			
			$result = mysql_query("select COUNT(number) as count from article_image 
									where autor_login like '$login'", $link);
			
			if (!$result) 
				throw new Instance_not_found_exception(get_class($this));
			
			$aux = mysql_fetch_assoc($result);
			
			Connection::singleton()->transaction_commit($link);
									
			return $aux['count'];
						
		}
		catch (Instance_not_found_exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw $e;
		}
		catch (Exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw new Internal_error_exception($e);
		}
	}
	
	public function update($article_image_to) {

		try {
				
			$link = Connection::singleton()->transaction_begin();
			
			$number = $article_image_to->get_number();
			$description = mysql_real_escape_string($article_image_to->get_description());
			$state = mysql_real_escape_string($article_image_to->get_state());
			
			$result = mysql_query("update article_image 
									set state='$state', description='$description'
									where number = '$number'", $link);
							
			if (!$result) 
				throw new Instance_not_found_exception(get_class($this));
			
			if (mysql_affected_rows($link) != 1) 
				throw new Sql_exception("Not row updated in " . get_class($this));
			
			
			Connection::singleton()->transaction_commit($link);			

		}
		catch (Sql_exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw new Internal_error_exception($e);		
		}
		catch (Instance_not_found_exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw $e;
		}
		catch (Exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw new Internal_error_exception($e);
		}	
		
	}
	
	public function remove($number) {	
		try {			
			$link = Connection::singleton()->transaction_begin();
			
			$result = mysql_query("delete from article_image
									where number = '$number'", $link);
									
			if (mysql_affected_rows($link) == 0) 
				throw new Instance_not_found_exception(get_class($this));
			
			if (mysql_affected_rows($link) != 1) 
				throw new Sql_exception("Not row removed in " . get_class($this));
			
			Connection::singleton()->transaction_commit($link);
					
		}
		catch (Sql_exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw new Internal_error_exception($e);		
		}
		catch (Instance_not_found_exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw $e;
		}
		catch (Exception $e){
			Connection::singleton()->transaction_rollback($link);
			throw new Internal_error_exception($e);
		}	
	}
}
?>